package com.atguigu.queue;

/**
 * @author shkstart
 * @create 2021-09-27 8:39
 */
public class QueueLinkedList {
    private int size;
    private Node head;//头节点
    private Node tail;//尾节点
    public QueueLinkedList(){  //初始化
        size = 0;
        head = null;
        tail = null;
    }
    class Node{
        public Object data;// 每个节点的数据
        public Node next;// 每个节点指向下一个节点的连接
        public Node(Object data) {
            this.data = data;
        }
    }
    public void insertTail(Object value){
        Node newNode = new Node(value);
        if (size == 0) {
            head = newNode;
        }else {
            tail.next = newNode;
        }
        tail = newNode;
        size++;
    }
    //删除头节点
    public Node delectHead(){
        if (size != 0) {
            head = head.next;
            size--;
//            如果删除之后为空，那就设置尾节点为空
            if (size == 0) {
                tail = null;
            }
            return head;
        }else {
            return null;
        }
    }
    //判断队列是否为空
    public boolean isNull() {
        return size == 0;
    }
    //打印
    public void printNode() {
        if (size > 0) {
            Node node = head;
            if (size == 1) {
                System.out.println("["+node.data+"]");
                return;
            }
            int item = size;
            System.out.print("[");
            while (item > 0) {
                if (node.next == null) {
                    System.out.print(node.data + "]");
                }else {
                    System.out.print(node.data + ",");
                }
                node = node.next;
                item--;
            }
        }else {
            System.out.println("[]");
        }
    }

    public static void main(String[] args) {
        QueueLinkedList queueLinkedList = new QueueLinkedList();
        System.out.println("添加的数据");
        queueLinkedList.insertTail(1);
        queueLinkedList.insertTail(2);
        queueLinkedList.insertTail(3);
        queueLinkedList.printNode();
        queueLinkedList.delectHead();
        System.out.println();
        System.out.println("出队列之后");
        queueLinkedList.printNode();;
    }

}
